#include "CSceneCamera.h"
#include "CTransform.h"
#include "ISceneNodeTransform.h"

BEGIN_WARE

CSceneCamera::CSceneCamera( uint32 uID )
: m_uID(uID)
, m_pParent(NULL)
, m_fAspc(0.0f)
, m_fFovy(0.0f)
, m_fNear(0.0f)
, m_fFar(0.0f)
, m_bVisible(VTRUE)
, m_bWireFrameMode(VFALSE)
{
}

CSceneCamera::~CSceneCamera(void)
{
}

int32 CSceneCamera::Drop()
{
	delete this;
	return 0;
}

VBOOL CSceneCamera::Update( IGraphic* pGraphic, uint32 uCurTime )
{
	if( !m_pParent )
		return VFALSE;

	CTransform& Trans = m_pParent->GetWorldTransform();
	Trans.GetTranslate( m_vPos );

	CMatrix3 m;
	Trans.GetRotate( m );
	m_vDir = m.GetColumn( 2 );
	m_vUp = m.GetColumn( 1 );

	m_vTarget = m_vPos + m_vDir;

	return VTRUE;
}

void CSceneCamera::CullVisibleObjects( IGraphic* pGraphic, ISceneNodeCamera* pCamera )
{

}

void CSceneCamera::Projection3D( CVector3& vPos, CVector3& vTarget, CVector3& vUp, float32 fAspc, float32 fFovy, float32 fNear, float32 fFar )
{
	m_vPos = vPos;
	m_vTarget = vTarget;
	m_vUp = vUp;

	m_vDir = m_vTarget - m_vPos;
	m_vDir.Normalize();

	m_fAspc = fAspc;
	m_fFovy = fFovy;
	m_fNear = fNear;
	m_fFar = fFar;

	SetParentNode();
}

void CSceneCamera::Orthogonal2D( CVector3& vPos, CVector3& vTarget, CVector3& vUp, float32 fNear, float32 fFar )
{
	m_vPos = vPos;
	m_vTarget = vTarget;
	m_vUp = vUp;

	m_vDir = m_vTarget - m_vPos;
	m_vDir.Normalize();

	m_fAspc = 0.0f;
	m_fFovy = 0.0f;
	m_fNear = fNear;
	m_fFar = fFar;

	SetParentNode();
}

void CSceneCamera::SetParentNode()
{
	if( !m_pParent )
		return;

	CTransform& worldTransform = m_pParent->GetWorldTransform();
	worldTransform.SetTranslate( m_vPos );

	CVector3 u, n, v;  /// x , y ,z
	u = m_vUp.Cross( m_vDir );
	u.Normalize();

	n = m_vDir.Cross( u );
	n.Normalize();

	CMatrix3 m;
	m.FromAxies( u, n, m_vDir );

	worldTransform.SetRotate( m );

	m_pParent->SetWorldTransform( worldTransform );
}

END_WARE
